$(window).ready(function() {
  
  


  function buildData (cate) {
    var table = $('table').get(0);
    var trs = $(table).find('tr');
    var table_name = 'table_name';
    
    let sql  = `CREATE TABLE test_user (
      id int NOT NULL AUTO_INCREMENT primary key,
      `;
    let django = ` id = models.BigAutoField(primary_key=True)  # ID
    `;
    let flask = `id = db.Column(db.Integer, primary_key=True)  #ID
    `

    for (let i = 1; i < trs.length; i++) {
      var field_name = $($(trs[i]).find('td').get(0)).text().trim()
      var comment = $($(trs[i]).find('td').get(1)).text().trim()
      var dataType = $($(trs[i]).find('td').get(2)).text().trim()
      var length = $($(trs[i]).find('td').get(3)).text().trim().replace(/,/g, '')
      var precision = $($(trs[i]).find('td').get(4)).text().trim(); // 精度
      var is_primary_key = $($(trs[i]).find('td').get(5)).text().trim()
      var is_not_null = $($(trs[i]).find('td').get(7)).text().trim()

      var curr_mysql_data_type_str = ''
      var curr_django_data_type_str = ''
      var curr_flask_data_type_str = ''
      if (dataType.startsWith('char')) {
        curr_mysql_data_type_str = `char(${length})`
        curr_django_data_type_str = `CharField(max_length=${length}, blank=True, null=True)  #${comment}`
        curr_flask_data_type_str = `db.String(${length})`
      } else if (dataType.startsWith('varchar')) {
        curr_mysql_data_type_str = `varchar(${length})`
        curr_django_data_type_str = `CharField(max_length=${length}, blank=True, null=True)  #${comment}`
        curr_flask_data_type_str = `db.String(${length})`
      } else if (dataType.startsWith('smallint')) {
        curr_mysql_data_type_str = `smallint`
        curr_django_data_type_str = `SmallIntegerField(blank=True, null=True)  #${comment}`
        curr_flask_data_type_str = `db.Integer`
      }else if (dataType.startsWith('decimal')) {
        curr_mysql_data_type_str = `decimal(${length}, ${precision})`
        curr_django_data_type_str = `DecimalField(max_digits=${length},decimal_places=${precision}, blank=True,  null=True)  #${comment}`
        curr_flask_data_type_str = `db.DECIMAL(${length}, ${precision})`
      }else if (dataType.startsWith('int')) {
        curr_mysql_data_type_str = `int`
        curr_django_data_type_str = `IntegerField(blank=True, null=True) #${comment}`
        curr_flask_data_type_str = `db.Integer`
      }else if (dataType.startsWith('numeric')) {
        curr_mysql_data_type_str = `decimal(${length}, ${precision})`
        curr_django_data_type_str = `DecimalField(max_digits=${length},decimal_places=${precision}, blank=True,  null=True)  #${comment}`
        curr_flask_data_type_str = `db.DECIMAL(${length}, ${precision})`
      }

      if (field_name.endsWith('date') || field_name.endsWith('time')) {
        curr_mysql_data_type_str = `datetime`
      }
      
      const mysql_field_item = `${field_name} ${curr_mysql_data_type_str} default null COMMENT '${comment}',
      `;
      
      sql += mysql_field_item;

      const django_field_item = `${field_name} = models.${curr_django_data_type_str} #${comment} 
      `;
      django += django_field_item

      const flask_field_item = `${field_name} = db.Column(${curr_flask_data_type_str}) #${comment}
      `
      flask += flask_field_item
    }

    sql += `dr int DEFAULT NULL,
      ts datetime(6) DEFAULT NULL
    );
    `
    if (cate === 'mysql') {
      console.log(sql)
    } else if (cate === 'django') {
      console.log(django)
    } else if (cate === 'flask') {
      console.log(flask)
    }
    
    
  }

  function buildBtn() {
    const btnHtml = `
    
    <div style="position:fixed; right: 10px; top: 10px">
      <p>点击按钮生成数据请打开控制台</p>
      <button id="buildMysql">生成 MySQL 表</button>
      <button id="buildDjango">生成 Django 表</button>
      <button id="buildFlask">生成 Falsk 表</button>
    </div>
    
    `;
    $('body').append(btnHtml);
    $('#buildMysql').on('click', function() {
      buildData('mysql');
    });
    $('#buildDjango').on('click', function() {
      buildData('django');
    });
    $('#buildFlask').on('click', function() {
      buildData('flask');
    });
  }

  buildBtn();
});